//
//  DownloadManager.m
//  OutesIOS
//
//  Created by norman on 2018/11/14.
//  Copyright © 2018 norman. All rights reserved.
//

#import "DownloadManager.h"
#import "ResourceDataResponse.h"

@implementation DownloadManager

+ (DownloadManager *)manager{
    static DownloadManager *_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        _manager = [[DownloadManager alloc] init];
    });
    return _manager;
}

- (void)checkFileAndUpdate {
    [HttpClient getWithUrl:HTTP_GET_RESOURCE_CHECK token:[[AccountManager manager]token] paramDic:nil resultClass:[ResourceDataResponse class] success:^(ResourceDataResponse *response) {
        if (response.result != nil) {
            
        }
        
    } failure:^(NSError *error) {
        CMLog(@"请求数据失败！，错误信息为%@",error);
    }];
}

- (void)updateFile:(ResourceInfoModel *)file {
    
}

- (BOOL)isFileExist:(NSString *)fileName {
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"db"];
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    if (result) {
//        [self renameProjectDir]
    }
    NSLog(@"这个文件已经存在：%@",result?@"是的":@"不存在");
    return result;
}

- (BOOL)renameProjectDir {
    NSString *projectDir = [NSString stringWithFormat:@"%@/Documents/product", NSHomeDirectory()];
    NSString *toDir = [NSString stringWithFormat:@"%@/Documents/product_bak", NSHomeDirectory()];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager moveItemAtPath:projectDir toPath:toDir error:nil];
    return result;
}

- (BOOL)rollbackProjectDir {
    NSString *projectDir = [NSString stringWithFormat:@"%@/Documents/product_bak", NSHomeDirectory()];
    NSString *toDir = [NSString stringWithFormat:@"%@/Documents/product", NSHomeDirectory()];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager moveItemAtPath:projectDir toPath:toDir error:nil];
    return result;
}


@end
